package com.his.modules.business.controller;

import com.his.core.entity.R;
import com.his.core.utils.StringUtils;
import com.his.modules.business.entity.*;
import com.his.modules.business.service.MedicalRecordService;
import com.his.modules.business.utils.MedicalRecordAnalysisUtilNew;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Controller
@Slf4j
@RequestMapping("/medicalRecord")
public class MedicalRecordController {
    @Autowired
    private MedicalRecordService medicalRecordService;

    /**
     * 单病历解析
     * @param medicalRecord
     * @return
     */
    @ResponseBody
    @PostMapping("analysis")
    public R analysis(@RequestBody MedicalRecord medicalRecord) {
        if(Objects.isNull(medicalRecord.getBlbh()) || medicalRecord.getBlbh() <= 0) {
            return R.failed("病历编号不合法");
        }
        if(StringUtils.isBlank(medicalRecord.getBlfl())) {
            return R.failed("病历分类不合法");
        }

        MedicalRecord mr = medicalRecordService.medicalRecordInfoMrr(medicalRecord);
        if(Objects.isNull(mr)) {
            return R.failed("未找到相应病历信息");
        }

        log.info("开始解析病历{}----{}=================", mr.getBlbh(), mr.getBlmc());
        try {
            String blnr = new String(mr.getBlnr(), "GBK");
            MedicalRecordAnalysis medicalRecordAnalysis = new MedicalRecordAnalysis();
            medicalRecordAnalysis.setBlbh(mr.getBlbh());
            medicalRecordAnalysis.setBlfl(mr.getBlfl());
            medicalRecordAnalysis.setBlmc(mr.getBlmc());
            medicalRecordAnalysis.setJxzt("1");
            medicalRecordAnalysis.setBllb(mr.getBllb());
            medicalRecordAnalysis.setYdlbbm(mr.getYdlbbm());
            medicalRecordAnalysis.setJzxh(mr.getJzxh());
            List<MedicalRecordAnalysisRecord> paraList = new ArrayList<>();
            List<MedicalRecordAnalysisRecord> medicalRecordAnalysisRecordList = MedicalRecordAnalysisUtilNew.medicalRecordAnalysis(blnr, medicalRecordAnalysis, paraList);
            if(!CollectionUtils.isEmpty(medicalRecordAnalysisRecordList)) {
                boolean flag = false;
                for(MedicalRecordAnalysisRecord medicalRecordAnalysisRecord : medicalRecordAnalysisRecordList) {
                    if(StringUtils.isBlank(medicalRecordAnalysisRecord.getYsqz())
                            || StringUtils.isBlank(medicalRecordAnalysisRecord.getYsmc())
                            || StringUtils.isBlank(medicalRecordAnalysisRecord.getDlmc())) {
                        continue;
                    }
                    flag = true;
                    medicalRecordAnalysisRecord.setBlbh(mr.getBlbh());
                    medicalRecordAnalysisRecord.setBllx(mr.getBllx());
                    medicalRecordAnalysisRecord.setBllb(mr.getBllb());
                    medicalRecordAnalysisRecord.setMblb(mr.getMblb());
                    medicalRecordAnalysisRecord.setMbbh(mr.getMbbh());
                    medicalRecordAnalysisRecord.setJzxh(mr.getJzxh());
                }
                if(!flag) {
                    log.info("病历解析异常，可能原因段落名称元素名称段落元素取值为空，{}----{}=================", mr.getBlbh(), mr.getBlmc());
                    return R.failed("病历解析异常，可能原因段落名称元素名称段落元素取值为空，" + mr.getBlbh() + "----" + mr.getBlmc());
                }
            }
            //保存解决数据
            medicalRecordService.medicalRecordAnalysisMrr(medicalRecordAnalysis, medicalRecordAnalysisRecordList, paraList);
        } catch (UnsupportedEncodingException e) {
            log.info("解析出错了！" + e.getMessage());
            return R.failed("解析出错了！" + e.getMessage() + mr.getBlbh() + "----" + mr.getBlmc());
        }

        return R.ok();
    }
}